package com.alumni_circle.core.web.rbac;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.rbac.Authority;
import com.alumni_circle.core.entity.rbac.RoleAuthority;
import com.alumni_circle.core.entity.rbac.UserRole;
import com.alumni_circle.core.entity.rbac.VO.RoleAuthoritySearchVO;
import com.alumni_circle.core.entity.rbac.VO.RoleAuthorityUpdateVO;
import com.alumni_circle.core.entity.rbac.VO.RoleAuthorityViewVO;
import com.alumni_circle.core.service.rbac.impl.AuthorityServiceImpl;
import com.alumni_circle.core.service.rbac.impl.RoleAuthorityServiceImpl;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.me.Authentication;
import com.alumni_circle.service.me.impl.AuthenticationServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙小虬
 * @since 2020-08-02 20:43
 */
@Api(tags = "角色权限管理")
@Controller
@RequestMapping("/roleAuthority")
public class RoleAuthorityController extends BaseController<RoleAuthority> {

    @Resource
    RoleAuthorityServiceImpl roleAuthorityService;

    @Resource
    AuthorityServiceImpl authorityService;


    /**
     * 更新角色权限信息
     * @param roleAuthorityUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateRoleAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新角色权限信息", notes = "更新角色权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新角色权限信息",url="/roleAuthority/updateRoleAuthority")
    public ResponseModel addUserAuthority(@ApiParam(required = true, name = "userRoleUpdateVO")
                                 @RequestBody RoleAuthorityUpdateVO roleAuthorityUpdateVO) {
        QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permissions",roleAuthorityUpdateVO.getPermissions());
        RoleAuthority roleAuthority = new RoleAuthority();
        roleAuthority.setAuthorityId(authorityService.getOne(queryWrapper).getId());
        roleAuthority.setId(roleAuthorityUpdateVO.getId());
        roleAuthorityService.updateById(roleAuthority);
       return new ResponseModel<>();
    }

    /**
     * 查询角色权限信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllRoleAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取角色权限信息", notes = "获取角色权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="获取角色权限信息",url="/roleAuthority/getAllRoleAuthority")
    public ResponseListModel getAllRoleAuthority(@RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<RoleAuthorityViewVO> list =roleAuthorityService.getAllRoleAuthority();
        PageInfo<RoleAuthorityViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 搜索角色权限信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchRoleAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索角色权限信息", notes = "搜索角色权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索角色权限信息",url="/roleAuthority/searchRoleAuthority")
    public ResponseListModel searchRoleAuthority(@ApiParam(required = true, name = "roleAuthoritySearchVO")
                                                 @RequestBody RoleAuthoritySearchVO roleAuthoritySearchVO) {
        System.out.println(roleAuthoritySearchVO);
        PageHelper.startPage(roleAuthoritySearchVO.getPageNum(),roleAuthoritySearchVO.getPageSize());
        List<RoleAuthorityViewVO> list = ModelUtils.copyListProperties(roleAuthorityService.searchRoleAuthority(roleAuthoritySearchVO.getId(), roleAuthoritySearchVO.getUrl(), roleAuthoritySearchVO.getRoleComment(), roleAuthoritySearchVO.getState()), RoleAuthorityViewVO.class);
        PageInfo<RoleAuthorityViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 删除角色权限信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/removeRoleAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除角色权限信息", notes = "删除角色权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除角色权限信息",url="/roleAuthority/removeRoleAuthority")
    public ResponseModel removeRoleAuthority(@ApiParam(required = true, name = "baseId")
                                             @RequestBody String id) {
        System.out.println(id);
        return new ResponseModel<>(roleAuthorityService.removeById(id));
    }


    /**
     * 禁用角色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/disRoleAuthority", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "禁用角色权限信息", notes = "禁用角色权限信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="禁用角色权限信息",url="/roleAuthority/disRoleAuthority")
    public ResponseModel disRoleAuthority(@ApiParam(required = true, name = "baseId")
                                             @RequestBody BaseId baseId) {
        RoleAuthority roleAuthority = new RoleAuthority();
        roleAuthority.setId(baseId.getId());
        roleAuthority.setState("1");
        return new ResponseModel<>(roleAuthorityService.updateById(roleAuthority));
    }

}
